﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThanhTu.GNCMedia.KQXS.DataAccess.DAL;
using System.Data;
using ThanhTu.GNCMedia.KQXS.DataAccess.DO;
using System.Text.RegularExpressions;

namespace WebUI.Admin
{
    public partial class AddData : System.Web.UI.Page
    {
        #region Feild
        ResultLotteryDAL resultLottery = null;
        ResultLotteryDO resultLotteryDO = null;
        string chooseKindOfAwards = null;
        #endregion
        #region Method

        private void LoadAllResultLottery()
        {
            ResultLotteryDAL resultLottery = new ResultLotteryDAL();

            grv_ResultLottery.DataSource = resultLottery.SelectALL_KQ_QT();
            grv_ResultLottery.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadAllResultLottery();
        }
        private bool checkTypeData(string idProvince, string idKindOfAward, string openDate, string result)
        {
            bool typeData = false;

            if (Regex.IsMatch(idProvince, "^[a-zA-Z]{1,6}$"))
            {
                typeData = true;
            }
            else
            {
                typeData = false;
            }
            if (Regex.IsMatch(result, "^[0-9]"))
            {
                typeData = true;
            }
            else
            {
                typeData = false;
            }

            if (Regex.IsMatch(idKindOfAward, "^[0-9]"))
            {
                typeData = true;
            }
            else
            {
                typeData = false;
            }
            return typeData;
        }
        private bool checkTypeData(string idProvince, string openDate)
        {
            bool checktype = false;
            if (Regex.IsMatch(idProvince, "^[a-zA-Z]{1,6}$"))
            {
                checktype = true;
            }
            else
            {
                checktype = false;
            }
            if (Regex.IsMatch(openDate, "^((19|20)\\d\\d)-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$"))
            {
                checktype = true;
            }
            else
            {
                checktype = false;
            }
            return checktype;
        }

        #endregion

        protected void lbtnLogout_Click(object sender, EventArgs e)
        {
            Session.Remove("UserName");
            Response.Redirect("Login.aspx");
        }

     

        protected void grv_ResultLottery_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            grv_ResultLottery.PageIndex = e.NewSelectedIndex;
            LoadAllResultLottery();
        }

        protected void grv_ResultLottery_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void grv_ResultLottery_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
        //    resultLottery = new ResultLotteryDAL();
        //    resultLotteryDO = new ResultLotteryDO();
        //    if (grv_ResultLottery.Rows.Count > 0)
        //    {
        //        GridViewRow grvRow = grv_ResultLottery.Rows[e.RowIndex];
        //        TextBox txt_idTinh = ((TextBox)grvRow.Cells[1].Controls[0]);
        //        string id_tinh = txt_idTinh.Text.ToString();
        //        TextBox txt_dateopen = ((TextBox)grvRow.Cells[3].Controls[0]);
        //        string date_Open = txt_dateopen.Text.ToString();
        //        if (!id_tinh.Trim().Equals("") || !date_Open.Trim().Equals(""))
        //        {

        //            resultLotteryDO.ProvinceID = id_tinh;
        //            resultLotteryDO.Opendate = Convert.ToDateTime(date_Open);
        //            int checkDel = resultLottery.delete(resultLotteryDO.ProvinceID, resultLotteryDO.Opendate);
        //            if (checkDel > 0)
        //            {
        //                //Lbl_Requiment.ForeColor = System.Drawing.Color.Red;
        //                //Lbl_Requiment.Text = "Delete Sucessful";
        //            }
        //            else
        //            {
        //                //Lbl_Requiment.ForeColor = System.Drawing.Color.Red;
        //                //Lbl_Requiment.Text = "Delete Error please try again";
        //            }
        //        }
        //        else
        //        {
        //            //Lbl_Requiment.ForeColor = System.Drawing.Color.Red;
        //            //Lbl_Requiment.Text = "Some Data is Empty ,Please input full data";
        }            
        

        protected void grv_ResultLottery_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //resultLottery = new ResultLotteryDAL();
            //resultLotteryDO = new ResultLotteryDO();
            //GridViewRow grvRow = grv_ResultLottery.Rows[e.RowIndex];
            //TextBox txt_idTinh = ((TextBox)grvRow.Cells[1].Controls[0]);
            //string id_tinh = txt_idTinh.Text.ToString();
            //TextBox txt_kindOfaward = ((TextBox)grvRow.Cells[2].Controls[0]);
            //string kindOfAward = txt_kindOfaward.Text.ToString();
            //TextBox txt_dateopen = ((TextBox)grvRow.Cells[3].Controls[0]);
            //string date_Open = txt_dateopen.Text.ToString();
            //TextBox txt_result = ((TextBox)grvRow.Cells[4].Controls[0]);
            //string result = txt_result.Text.ToString();
            //if (!id_tinh.Trim().Equals("") || !kindOfAward.Trim().Equals("") || !date_Open.Trim().Equals("") || !result.Trim().Equals(""))
            //{
            //    if (checkTypeData(id_tinh, kindOfAward, date_Open, result))
            //    {
            //        resultLotteryDO.ProvinceID = id_tinh;
            //        resultLotteryDO.KindOfawards = Convert.ToInt32(kindOfAward);
            //        resultLotteryDO.Opendate = Convert.ToDateTime(date_Open);
            //        resultLotteryDO.Result = result;

            //        int checkUpdateResult = resultLottery.add(resultLotteryDO);
            //        if (checkUpdateResult > 0)
            //        {
            //            Lb_Notice.ForeColor = System.Drawing.Color.Red;
            //            Lb_Notice.Text = "Update Successful !";
            //            grv_ResultLottery.EditIndex = -1;
            //        }
            //        else
            //        {
            //            Lb_Notice.ForeColor = System.Drawing.Color.Red;
            //            Lb_Notice.Text = "Update Not Success , please try again";
            //        }
            //    }
            //    else
            //    {
            //        Lb_Notice.ForeColor = System.Drawing.Color.Red;
            //        Lb_Notice.Text = " Fomat data is Wrong ,Please input correct data input !";
            //    }
            //}
            //else
            //{
            //    Lb_Notice.ForeColor = System.Drawing.Color.Red;
            //    Lb_Notice.Text = "Some Data is Empty ,Please input full data";
            //}
        }

        protected void grv_ResultLottery_RowEditing(object sender, GridViewEditEventArgs e)
        {
            //grv_ResultLottery.EditIndex = e.NewEditIndex;
            //LoadAllResultLottery();

        }

        protected void grv_ResultLottery_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            //grv_ResultLottery.EditIndex = -1;
            //LoadAllResultLottery();
        }

        protected void btn_addNewData_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddnewData.aspx");
        }

    }
}